package com.wteam.dragon.system.apps.server.controller;

import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.apps.server.povo.bean.Server;
import com.wteam.dragon.system.apps.server.service.ServerService;
import com.wteam.dragon.system.apps.server.povo.vo.ServerVo;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 陈晓辉
 * @Date 2020/3/23
 */
@RestController
@Api(tags = "系统: 服务器管理")
@RequestMapping("admin/server")
@RequiredArgsConstructor
public class ServerController {
    private final ServerService serverService;

    @Log("测试连接服务器")
    @ApiOperation("测试连接服务器")
    @PostMapping("testConnect")
    @PreAuthorize("@el.check('testConnect')")
    public ResultMessage testConnect(ServerVo serverVo){
        Server server = BeanUtil.from(serverVo, Server.class);
        return serverService.testConnect(server);
    }

    @Log("添加服务器")
    @ApiOperation("添加服务器")
    @PostMapping("addServer")
    @PreAuthorize("@el.check('addServer')")
    public ResultMessage addServer(ServerVo serverVo){
        Server server = BeanUtil.from(serverVo, Server.class);
        return serverService.addServer(server);
    }

    @Log("删除服务器")
    @ApiOperation("删除服务器")
    @DeleteMapping("delServer")
    @PreAuthorize("@el.check('delServer')")
    public ResultMessage delServer(Long id){
        return serverService.delServer(id);
    }

    @Log("更新服务器")
    @ApiOperation("更新服务器")
    @PutMapping("editServer")
    @PreAuthorize("@el.check('editServer')")
    public ResultMessage editServer(ServerVo serverVo){
        Server server = BeanUtil.from(serverVo, Server.class);
        return serverService.editServer(server);
    }

    @Log("查询服务器列表")
    @ApiOperation("查询服务器列表")
    @GetMapping("listServer")
    @PreAuthorize("@el.check('listServer')")
    public ResultMessage listServer(){
        return serverService.listServer();
    }
}
